package collection.map;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class TestHashMap {

    public static void main(String[] args){
        int num = 1 << 8;
        System.out.println(num);
//        Map<String, String> m = new HashMap<>();
//        m.put("1", "12");
//        Map<String, String> map = new HashMap<>((int)(num/0.75)+1);
//        map.put("1", "12");
//        System.out.println(map.size());
        put(num);
        putByNum(num);
    }

    private static void put(int num){
        Random r = new Random();

        long b1 = System.currentTimeMillis();
        Map<String, String> map = new HashMap<>();
        for (int i=0; i<num; i++){
            String k = String.valueOf(31*i*r.nextInt(1000));
            map.put(k,k);
        }
        long e1 = System.currentTimeMillis();
        System.out.println( "put time 1：" + (e1-b1) );

        long b2 = System.currentTimeMillis();
        for (String key : map.keySet()){
            String val = map.get(key);
        }
        long e2 = System.currentTimeMillis();
        System.out.println( "put time 2：" + (e2-b2) );

    }

    private static void putByNum(int num){
        Random r = new Random();

        long b1 = System.currentTimeMillis();
        Map<String, String> map = new HashMap<>(num);
        for (int i=0; i<num; i++){
            String k = String.valueOf(31*i*r.nextInt(1000));
            map.put(k,k);
        }
        long e1 = System.currentTimeMillis();
        System.out.println( "putByNum time 1：" + (e1-b1) );

        long b2 = System.currentTimeMillis();
        for (String key : map.keySet()){
            String val = map.get(key);
        }
        long e2 = System.currentTimeMillis();
        System.out.println( "putByNum time 2：" + (e2-b2) );

    }

}
